<template>
	<view>
    <view class="box">
      <view class="box-title">
          <image class="logo" src="https://www.iwangbang.com/skin/wx_mp_aixz/logo.png"></image>
          <view class="">
            小帮观影
          </view>
      </view>
    </view>
    <button class="getlogin" @click="wxlogin">一键登录</button>
		<!-- <button v-if="thefirst" class="getlogin"  open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" >一键登录</button>
		<button v-else class="userlogin" @click="getUserProfile()">首次注册获取用户资料</button> -->
	</view>   
</template>

<script >
	export default {
   onUnload(e){
       	//正常返回页面
       	wx.reLaunch({
       			url:"/pages/index/index"
       		})
       },
		data() {
			return {
				username:'用户名',
				headimg:'',
				tel:'未绑定手机号',
				unionid:'',
				openid:'',
        thefirst:true,
        inviteCode:''
			}
		},
    onShow() {
      console.log(uni.getStorageSync('wbyun_inviteCode'));
      if(uni.getStorageSync('wbyun_inviteCode') == '' || uni.getStorageSync('wbyun_inviteCode') == null || uni.getStorageSync('wbyun_inviteCode') == undefined) {
        this.inviteCode = ''
      }else {
        this.inviteCode = uni.getStorageSync('wbyun_inviteCode')
      }
    },
   
		methods: {
      
			getPhoneNumber(e) {
        console.log(123);
				var that = this; 
				console.log(e);
				console.log(e.detail.code) // 动态令牌
				console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
				console.log(e.detail.errno) // 错误码（失败时返回）
				wx.request({
					url: getApp().globalData.apiDomain + 'user/getPhoneNumber',
					data: {
						code: e.detail.code
					},
					success(data) {
						console.log(data['data']);
						console.log(data['data']['datas']);
						var data = data['data']['datas'];
						console.log(data);
						console.log('登录成功'+data['token']);
						uni.setStorageSync('wbyun_token', data['token']);
						uni.setStorageSync('wbyun_username', data['username']);
						uni.setStorageSync('wbyun_headimg', data['headimg']);
						uni.setStorageSync('wbyun_tel', data['tel']);
						that.tel = data['tel'];
						console.log('手机号',that.tel);
						if(data['status']=='login'){
							 uni.navigateBack();
						}else{
							//获取用户资料
              that.thefirst = false
						}
					}
				})
			},
			getUserProfile() {
				console.log('xxx');
				// 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
				// 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
				wx.getUserProfile({
					desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
					success: (res) => {
						console.log(res.userInfo.nickName,res.userInfo.avatarUrl);
						
						wx.request({
							url: getApp().globalData.apiDomain + 'user/updateWxUserInfo',
              header: {
              	'token':uni.getStorageSync('wbyun_token'),
                'wbapp':'weixin_mp_xbgy'
              },
							method:'POST',
							data: {
								'username': res.userInfo.nickName,
								'headimg':res.userInfo.avatarUrl
							},
							success(data) {
								console.log(data);
                if(data.code == 200) {
                  // uni.navigateBack();
                }
								
							}
						})
					}
				})
			},
			
			wxlogin() {
				console.log('微信登录');
        console.log(this.inviteCode);
				var that = this;
				wx.login({
					success(res) {
						if (res.code) {
							console.log(res.code);
							//发起网络请求
							wx.request({
								url: getApp().globalData.apiDomain+'user/getWxOpenId',
                header: {
                    'content-type': 'application/x-www-form-urlencoded' ,
                    'wbapp':'weixin_mp_xbgy'
                },
								data: {
									code: res.code

								},
								success(data) {
                  console.log(data);
									var nd = data['data']['datas'];
									console.log(nd);
                  
									if(nd['status']=='register'){
                    console.log('未注册',nd['openid'],nd['unionid']);
                    uni.request({
                    	url: getApp().globalData.apiDomain+'user/updateWxUserInfo',
                    	method: 'POST',
                      header: {
                          'content-type': 'application/x-www-form-urlencoded' ,
                          'wbapp':'weixin_mp_xbgy'
                      },
                    	data:{
                    		'openid':nd['openid'],
                    		'unionid':nd['unionid'],
                        'icode':that.inviteCode
                        
                    	},
                    	dataType:'json',
                    	success: res => {
                    		console.log(res);
                    		if(res['data']['code']==200){
                    			uni.setStorageSync('wbyun_token', res.data.datas.token);
                    			uni.setStorageSync('wbyun_username', res.data.datas.username);
                    			uni.setStorageSync('wbyun_headimg', res.data.datas.headimg);
                    			uni.setStorageSync('wbyun_tel', res.data.datas.tel);
                          uni.setStorageSync('wbyun_icode', res.data.datas.icode);
                          console.log('11111');
                          uni.navigateBack();
                          // uni.navigateTo({
                          //   url:'/pages/index/index'
                          // })
                    		}else{
                    			uni.showToast({
                    				title: res['data']['msg'],
                    				icon: 'none'
                    			});
                    		}
                    	},
                    	fail: () => {},
                    	complete: () => {
                    		
                    	}
                    });
						
										return;
									}else if(nd['status']=="login") {
                    console.log(nd['token']);
                    uni.setStorageSync('wbyun_token', nd.token);
                    uni.setStorageSync('wbyun_username', nd.username);
                    uni.setStorageSync('wbyun_headimg', nd.headimg);
                    uni.setStorageSync('wbyun_tel', nd.tel);
                    uni.setStorageSync('wbyun_icode', nd.icode);
                    console.log('2222');
                    wx.navigateBack();
                    // wx.navigateTo({
                    //   url:'/pages/index/index'
                    // })
                  }
         //          wx.navigateBack();
									// console.log('登录成功'+nd['token']);
									// uni.setStorageSync('wbyun_token', nd['token']);
									// uni.setStorageSync('wbyun_username', nd['username']);
									// uni.setStorageSync('wbyun_headimg', nd['headimg']);
									// uni.setStorageSync('wbyun_tel', nd['tel']);
         //          uni.setStorageSync('wbyun_icode', nd.icode);
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			
		}
	}
</script>

<style>
  body {
    background-color: rgb(248,248,248);
  }
  .getlogin {
    width: 90%;
    margin: auto;
    color: #fff;
    background-image: linear-gradient(to right,rgb(81,109,250), rgb(245,112,243)) !important;
  }
  .userlogin {
    width: 90%;
    margin: auto;
    color: #fff;
    background-image: linear-gradient(to right,rgb(81,109,250), rgb(245,112,243)) !important;
  }
.logo {
  width: 150rpx;
  height: 150rpx;
  border-radius: 20rpx;
}
.box {
  height: 400rpx;
  text-align: center;
}
.box-title {
  margin-top: 80rpx;
}
</style>